package com.dragon.manage.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.AgentFinance;
import com.dragon.manage.bean.AgentInfo;
import com.dragon.manage.bean.AgentLevel;
import com.dragon.manage.bean.AgentLogs;
import com.dragon.manage.bean.ManageUser;
import com.dragon.manage.bean.Recharge;
import com.dragon.manage.bean.UserInfo;
import com.dragon.manage.dao.IAgentFinanceDAO;
import com.dragon.manage.dao.IAgentInfoDAO;
import com.dragon.manage.dao.IAgentLevelDAO;
import com.dragon.manage.dao.IAgentLogsDAO;
import com.dragon.manage.dao.IRechargeDAO;
import com.dragon.manage.dao.IUserAgentDAO;
import com.dragon.manage.dao.IUserInfoDAO;
import com.dragon.manage.service.IManageAgentService;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.TimeUtil;

public class ManageAgentServiceImpl implements IManageAgentService{

	private IAgentInfoDAO agentInfoDAO;
	
	private IUserInfoDAO userInfoDAO;
	
	private IUserAgentDAO userAgentDAO;
	
	private IAgentLogsDAO agentLogsDAO;
	
	private IAgentFinanceDAO agentFinanceDAO;
	
	private IAgentLevelDAO agentLevelDAO;
	
	private IRechargeDAO rechargeDAO;
	
	public void setRechargeDAO(IRechargeDAO rechargeDAO) {
		this.rechargeDAO = rechargeDAO;
	}
	
	public void setAgentLevelDAO(IAgentLevelDAO agentLevelDAO) {
		this.agentLevelDAO = agentLevelDAO;
	}
	
	public void setAgentFinanceDAO(IAgentFinanceDAO agentFinanceDAO) {
		this.agentFinanceDAO = agentFinanceDAO;
	}
	
	public void setAgentLogsDAO(IAgentLogsDAO agentLogsDAO) {
		this.agentLogsDAO = agentLogsDAO;
	}
	
	public void setAgentInfoDAO(IAgentInfoDAO agentInfoDAO) {
		this.agentInfoDAO = agentInfoDAO;
	}
	
	public void setUserInfoDAO(IUserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}
	
	public void setUserAgentDAO(IUserAgentDAO userAgentDAO) {
		this.userAgentDAO = userAgentDAO;
	}
	@Override
	public JSONObject agentList(int page) throws Exception {
		JSONObject reJ = new JSONObject();
		List<AgentInfo> agentList = agentInfoDAO.getAgentInfoList(page);
		JSONArray jsA = new JSONArray();
		for(AgentInfo tmp:agentList){
			UserInfo userInfo = userInfoDAO.getUserInfoByID(tmp.getUserid());
			if(userInfo==null){
				continue;
			}
			AgentLevel agentLevel = agentLevelDAO.getAgentLevelByID(tmp.getLevelid());
			jsA.add(agentInfoToPage(tmp, userInfo,agentLevel, userAgentDAO));
		}
		reJ.put("AgentBean", jsA);
		if(page!=StaticData.no_param){
			reJ.put(StaticData.reKey.total.getKey(), agentInfoDAO.getAgentInfoCount());
		}
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJ;
	}
	
	/**
	 * 把代理商的数据转换成前台需要的格式
	 * @param agentInfo
	 * @param userInfoDAO
	 * @param userAgentDAO
	 * @return
	 */
	private JSONObject agentInfoToPage(AgentInfo agentInfo,UserInfo userInfo,AgentLevel agentLevel,
			IUserAgentDAO userAgentDAO){
		JSONObject reJ = new JSONObject();
		reJ.put("nickname", userInfo.getNickname());
		reJ.put("agentId", agentInfo.getAgentid());
		//发展代理数量
		reJ.put("haveUserNum", userAgentDAO.getUserCount(agentInfo.getAgentid()));
		reJ.put("name", agentInfo.getName());
		reJ.put("phone", agentInfo.getPhone());
		if(agentLevel!=null){
			reJ.put("levelname", agentLevel.getLevelname());
		}else{
			reJ.put("levelname", "levle_error");
		}
		
		//渠道累计充值
		reJ.put("upgradeprice", agentInfo.getAmount());
		reJ.put("createtimeStr", TimeUtil.getDateFormat(agentInfo.getCreatetime()));
		reJ.put("cashAmount", agentInfo.getCashamount());
		reJ.put("agentStatus", agentInfo.getState());
		return reJ;
	}

	@Override
	public JSONObject lockAgent(int agentId,int type) throws Exception {
		JSONObject reJ = new JSONObject();
		AgentInfo agentInfo = agentInfoDAO.getAgentInfoByID(agentId);
		if(agentInfo==null){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJ;
		}
		
		if(type!=StaticData.userType.black.getType()&&type!=StaticData.userType.normal.getType()){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJ;
		}
		
		agentInfo.setState(type);
		agentInfoDAO.updateAgentInfo(agentInfo);
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJ;
	}

	@Override
	public JSONObject getAgentWithdrawList(int agentId,int type,int page) throws Exception {
		JSONObject reJ = new JSONObject();
		List<AgentFinance> list = agentFinanceDAO.getAgentFinanceList(agentId, 
				type,page);
		JSONArray jsA = new JSONArray();
		for(AgentFinance tmp:list){
			AgentInfo agentInfo = agentInfoDAO.getAgentInfoByID(tmp.getAgentid());
			if(agentInfo==null){
				continue;
			}
			jsA.add(turnAgentFinanceToPage(tmp,agentInfo));
		}
		if(page!=StaticData.no_param){
			reJ.put(StaticData.reKey.total.getKey(),
					agentFinanceDAO.getAgentFinanceCount(agentId, type));
		}
		reJ.put("financeList", jsA);
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJ;
	}

	private JSONObject turnAgentFinanceToPage(AgentFinance agentFinance,AgentInfo agentInfo){
		JSONObject reJ = new JSONObject();
		reJ.put("financeid", agentFinance.getFinanceid());
		reJ.put("agentId", agentFinance.getAgentid());
		reJ.put("price", agentFinance.getPrice());
		reJ.put("name", agentInfo.getName());
		reJ.put("phone", agentInfo.getPhone());
		reJ.put("remarks", agentFinance.getRemarks());
		reJ.put("auditremarks", agentFinance.getAuditremarks());
		reJ.put("status", agentFinance.getState());
		reJ.put("createDate",TimeUtil.getDateFormat(agentFinance.getCreatetime()));
		return reJ;
	}

	@Override
	public JSONObject getAgentEarningsDetail(int agentId,int page) throws Exception {
		JSONObject reJ = new JSONObject();
		List<AgentLogs> list = agentLogsDAO.getAgentLogsList(agentId, 
				StaticData.no_param,page);
		JSONArray jsA = new JSONArray();
		AgentInfo gAgentInfo = null;
		UserInfo gUserInfo = null;
		Recharge recharge = null;
		for(AgentLogs tmp:list){
			gAgentInfo = agentInfoDAO.getAgentInfoByID(tmp.getAgentid1());
			recharge = rechargeDAO.getRechargeByID(tmp.getRechargeid());
			if(recharge!=null){
				gUserInfo = userInfoDAO.getUserInfoByID(recharge.getUserId());
			}
			
			if(gUserInfo!=null){
				jsA.add(turnAgentLogsToPage(tmp, gAgentInfo, gUserInfo));
			}
			
		}
		reJ.put("agentLogs", jsA);
		
		if(page!=StaticData.no_param){
			reJ.put(StaticData.reKey.total.getKey(), agentLogsDAO.getAgentLogsCount(agentId,
					StaticData.no_param));
		}
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJ;
	}
	
	private JSONObject turnAgentLogsToPage(AgentLogs agentLogs,AgentInfo gAgentInfo,
			UserInfo gUserInfo){
		JSONObject reJ = new JSONObject();
		reJ.put("agentId", agentLogs.getAgentid());
		if(gAgentInfo!=null){
			reJ.put("gAgentName", gAgentInfo.getName());
		}
		reJ.put("gUserName", gUserInfo.getNickname());
		reJ.put("rechargeid", agentLogs.getRechargeid());
		reJ.put("price", agentLogs.getPrice());
		reJ.put("createDate",TimeUtil.getDateFormat(agentLogs.getCreatetime()));
		return reJ;
	}

	@Override
	public JSONObject checkWithdraw(int financeid, int type,ManageUser manageUser,String auditremarks) throws Exception {
		JSONObject reJ = new JSONObject();
		AgentFinance agentFinance = agentFinanceDAO.getAgentFinanceByID(financeid);
		if(agentFinance==null){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJ;
		}
		
		if(agentFinance.getState()!=StaticData.AgentFinanceType.wait_check.getType()){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.AgentFinance_status_error.getType());
			return reJ;
		}
		agentFinance.setState(type);
		agentFinance.setAudittime(TimeUtil.getSysteCurTime());
		if(type==StaticData.AgentFinanceType.reject.getType()){
			agentFinance.setAuditremarks(manageUser.getId()+":"+auditremarks);
		}else{
			agentFinance.setAuditremarks(manageUser.getId()+"");
			
			/**
			 * 提现审核成功，生成用户记录
			 */
			AgentLogs agentLogs = new AgentLogs();
			agentLogs.setAgentid(agentFinance.getAgentid());
			agentLogs.setAgentid1(StaticData.no_param);
			agentLogs.setRechargeid(StaticData.no_param);
			agentLogs.setPrice(agentFinance.getPrice());
			agentLogs.setCreatetime(TimeUtil.getSysteCurTime());
			agentLogs.setType(StaticData.AgentMoneyType.outgoings.getType());
			agentLogsDAO.createAgentLogs(agentLogs);
		}
		
		agentFinanceDAO.updateAgentFinance(agentFinance);
		
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJ;
	}

	@Override
	public JSONObject updateAgentInfo(int agentId, String phone, String name)
			throws Exception {
		JSONObject reJ = new JSONObject();
		AgentInfo agentInfo = agentInfoDAO.getAgentInfoByID(agentId);
		if(agentInfo==null){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJ;
		}
		if(phone!=null&&!phone.isEmpty()){
			agentInfo.setPhone(phone);
		}
		if(name!=null&&!name.isEmpty()){
			agentInfo.setName(name);
		}
		agentInfoDAO.updateAgentInfo(agentInfo);
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJ;
	}
}
